<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\UserGroupsBundle\Model;

use Base\BaseModel;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Model\UserGroupsBundle\Setting\UserGroupsSetting;
use Symfony\Component\HttpFoundation\Request;

class UserGroupsModel extends BaseModel{


    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new UserGroupsSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllUserGroups(){
        $qb = $this->getQueryBuilder();
        $qb->select("*")
        ->from("UserGroups");
        return $qb->execute()->fetchAll();
    }

    //获取所有分类并构建分类树
    public function getAllUserGroupsTree()
    {
        $categories = $this->getAllUserGroups();
        $UserGroupsTree = $this->buildeCatTreeArray($categories);
        $data = array();
        foreach ($UserGroupsTree as $class) {
            $prefix = "";
            if ($class["level"] > 0) {
                $prefix .="├";
                for ($i=0;$i<$class["level"];$i++ ) {
                    $prefix .="─";
                }
            }
            $data[] = array(
                "id"=>$class["id"],
                "name"=>$prefix.$class["name"],
            );
        }
        return $data;
    }

    //获取第一层分类
    public function getRootUserGroups()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->from("UserGroups");
        return $qb->execute()->fetchAll();
    }
}